//  File:         TagSpec.cpp
//  Author:       Scott Nordmann & Lauren Wallace
//  Description:  Non-inline member function definitions.
#include "TagSpec.h"
#include "Task.h"
#include "Event.h"
#include "Note.h"
#include <string>
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;
TagSpec::TagSpec()
{
}
void TagSpec::shutDown(ofstream& ofsave)
{
	ofsave << tags.size() << endl;
	if (tags.size() != 0)
	{
		vector<Tag*>::iterator iter;
		for(iter=tags.begin();iter !=tags.end(); ++iter)
		{
			Entry* entry = (*iter)->getEntry();

			if (dynamic_cast<Task*>(entry))
			{
				ofsave << "Task" << endl;
			}
			else if (dynamic_cast<Event*>(entry))
			{
				ofsave << "Event" << endl;
				ofsave << dynamic_cast<Event*>(entry)->getDateString() << endl;
			}
			else
			{
				ofsave << "Note" << endl;
			}

			ofsave << (entry)->getTitle() << endl;
		}
	}
}
ostream& operator<<(ostream& os, const TagSpec& T)
{
    T.display();
    return os;
}

void TagSpec::setName(const string nam)
{
	name= nam;
}

void TagSpec::display()const
{
	cout << getName() << " uses:" << tags.size();
}

void TagSpec::displayEntries()const
{
	Entry* currentEntry;
	int ctr=0;
	int choice=0;
	vector<Tag*>::const_iterator iter;
	for (iter=tags.begin();iter != tags.end(); ++iter)
	{
		++ctr;
		currentEntry = (*iter)->getEntry();
		cout << "[" <<ctr<< "]";
		currentEntry->displayTitle();
		cout << endl;
	}
	cout << "Which Entry would you like to view (enter number 1 - " << ctr << ")? ";
	cin >> choice;
	// range checked here- if it's out of bounds put choice in bounds
	if (choice<1)
		choice = 1;
	if (choice > ctr)
		choice = ctr;
			
	--choice;//decriment choice so that it works with vector
	currentEntry = tags.at(choice)->getEntry();
	currentEntry->display();
}

void TagSpec::populate(const string& nam)
{
	setName(nam);
}

Tag* TagSpec::halfTag()
{
	Tag* currentTag = new Tag(*this);
	tags.push_back(currentTag);
	return currentTag;
}

void TagSpec::removeHalfTag(Tag& tag)
{
	vector<Tag*>::const_iterator iter;
	for(iter=tags.begin(); iter != tags.end(); ++iter)
	{
		if (tag.getEntry()==(*iter)->getEntry())
		{
			delete (*iter);
			tags.erase(iter);
			break;
		}
	}
}

void TagSpec::removeTag(Tag* tag)
{
	vector<Tag*>::iterator iter;
	
	for ( iter=tags.begin();iter != tags.end(); ++iter )
	{
		if ( (*iter)->getEntry() == tag->getEntry() )
		{
			tags.erase(iter);
			break;
		}
	}
}